<?php
namespace app\controller;

use app\BaseController;
use think\Token;

class Recycle extends BaseController
{
    public function __construct(\think\App $app, Token $token)
    {
        $this->app = $app;
        $this->request = $this->app->request;

        $this->token = $token;

        $this->ProjectModel = new \app\common\model\Project;
        $this->FilesModel = new \app\common\model\Files;
    }

    // 获取回收站列表
    public function list()
    {
        if ($this->request->isGet()) {
            $token = $this->request->param('token', '', 'trim');

            if (empty($token)) {
                return json(error_ful('账号不存在'));
            }

            $userInfo = $this->token->checkToken($token);
            $weigh = $userInfo['data']['rank']['weigh'];
            if ($weigh !== '99') {
                return json(error_ful('您的账号无权限'));
            }

            $projecList = $this->ProjectModel->where('deletetime', '<>' . null)->select();
            $fileList = $this->FilesModel->where('deletetime', '<>', null)->select();
            $data = [
                'project_list' => $projecList,
                'file_list' => $fileList
            ];
            return json(success_ful('返回数据', $data));
        }
    }

    // 还原
    public function reduce()
    {
        if ($this->request->isGet()) {
            $token = $this->request->param('token', '', 'trim');
            $proid = $this->request->param('proid', 0, 'trim');
            $fileid = $this->request->param('fileid', 0, 'trim');

            // 解码token
            // 判断账号权限
            if (empty($token)) {
                return json(error_ful('账号不存在'));
            }

            $userInfo = $this->token->checkToken($token);
            if($userInfo['status'] == 0){
                return json(error_ful($userInfo['message']));
            }
            $weigh = $userInfo['data']['rank']['weigh'];
            if ($weigh !== '99') {
                return json(error_ful('您的账号无权限'));
            }

            if (!empty($proid) && !empty($fileid)) {
                return json(error_ful('文件不明确'));
            }

            if (!empty($proid)) {
                $status = $this->ProjectModel->where('id', $proid)->update(['deletetime' => null]);
                if ($status === false || $status == 0) {
                    return json(error_ful('还原失败'));
                } else {
                    return json(success_ful('还原成功'));
                }
            } elseif (!empty($fileid)) {
                $status = $this->FilesModel->where('id', $fileid)->update(['deletetime' => null]);
                if ($status === false || $status == 0) {
                    return json(error_ful('还原失败'));
                } else {
                    return json(success_ful('还原成功'));
                }
            } else {
                return json(error_ful('文件不存在'));
            }
        }
    }

    // 销毁
    public function destroy()
    {
        if ($this->request->isGet()) {
            $token = $this->request->param('token', '', 'trim');
            $proid = $this->request->param('proid', 0, 'trim');
            $fileid = $this->request->param('fileid', 0, 'trim');

            // 解码token
            // 判断账号权限
            if (empty($token)) {
                return json(error_ful('账号不存在'));
            }

            // 验证token
            $userInfo = $this->token->checkToken($token);
            if($userInfo['status'] == 0){
                return json(error_ful($userInfo['message']));
            }
            $weigh = $userInfo['data']['rank']['weigh'];
            if ($weigh !== '99') {
                return json(error_ful('您的账号无权限'));
            }

            if (!empty($proid) && !empty($fileid)) {
                return json(error_ful('文件不明确'));
            }

            if (!empty($proid)) {
                $status = $this->ProjectModel->where('id', $proid)->delete();
                if ($status === false || $status == 0) {
                    return json(error_ful('销毁失败'));
                } else {
                    return json(success_ful('销毁成功'));
                }
            } elseif (!empty($fileid)) {
                $status = $this->FilesModel->where('id', $fileid)->delete();
                if ($status === false || $status == 0) {
                    return json(error_ful('销毁失败'));
                } else {
                    return json(success_ful('销毁成功'));
                }
            } else {
                return json(error_ful('文件不存在'));
            }
        }
    }
}